//
//  LKSportTrackViewController.m
//  SportDemo
//
//  Created by Lukj on 2017/4/23.
//  Copyright © 2017年 lukj. All rights reserved.
//

#import "LKSportTrackViewController.h"
#import "LKSportMapViewController.h"
#import "LKAnimation.h"
#import "LKSportMapModeViewController.h"
@interface LKSportTrackViewController ()<UIPopoverPresentationControllerDelegate>
@property (nonatomic, strong) LKSportMapViewController *mapViewVC;

@property (nonatomic, strong) LKAnimation *animation;

@property (weak, nonatomic) IBOutlet UILabel *distanceLabel;

@property (weak, nonatomic) IBOutlet UILabel *timeLabel;

@end

@implementation LKSportTrackViewController
- (instancetype)init
{
    self = [super init];
    if (self) {
        //  设置地图
        [self setupMapView];
        //  设置自定义展示样式
        self.modalPresentationStyle = UIModalPresentationCustom;
        //  强引用动画器
        self.animation = [LKAnimation new];
        self.transitioningDelegate = self.animation;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //  接收通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(locationDidUpdateNote) name:@"LKSportLoactionDidUpdateNote" object:nil];
    
    self.timeLabel.font = [UIFont fontWithName:@"DINCond-Bold" size:30];
}

//  位置更新后响应
- (void)locationDidUpdateNote {
    //  设置数据
    self.distanceLabel.text = [NSString stringWithFormat:@"%.2f", _track.totalDistace];
    
    int time = (int)self.track.totalTime;
    self.timeLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", time / 3600, (time % 3600) / 60, time % 60];
}


- (void)setupMapView {

    
    //使用XIB时,在ViewDidLoad中的视图尺寸和XIB中预览的尺寸一致(视图的尺寸还没有同步设备的真实尺寸)
    
    //创建mapVc
    LKSportMapViewController *mapViewVC = [[LKSportMapViewController alloc] init];
    //使用父子控制器的方式
    //1.添加视图控制器(不添加也可以显示,但是响应者链条断开)    SB添加视图控制器使用ContainerView
    [self addChildViewController:mapViewVC];
    //2.添加内容视图为子视图
    [self.view insertSubview:mapViewVC.view atIndex:0];
    //3.设置子视图的尺寸 添加子视图时一定要设置其尺寸,避免出现autoresizing的错误处理
    mapViewVC.view.frame = self.view.bounds;
    //官方推荐
    [mapViewVC didMoveToParentViewController:self];
    self.mapViewVC = mapViewVC;

    
}

#pragma mark - 响应事件
//  点击关闭按钮
- (IBAction)clickCloseBtn:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

//  点击选择地图类型
- (IBAction)clickMapModBtn:(UIButton *)sender {
    //  Popover 弹出窗, 是modal的一种
    //  创建地图类型控制器
    LKSportMapModeViewController *mapModeVc = [[LKSportMapModeViewController alloc] initWithSelectBlock:^(MapSelectType mapSelectType) {
        //传递地图类型给mapVc
        self.mapViewVC.mapSelectType = mapSelectType;
    }];
    
    mapModeVc.mapSelectType = self.mapViewVC.mapSelectType;
    
    //  modal展示样式 ios会自动检测设备类型, 如果是iPhone上使用Popover, 还是会使用默认的的modal样式
    mapModeVc.modalPresentationStyle = UIModalPresentationPopover;
    //  iPhone上使用Popover的解决办法: 取消系统的默认modal样式, 需要使用Popover的代理
    //  获取Popover对象, 用于设置Popover相关的数据
    UIPopoverPresentationController *popover = mapModeVc.popoverPresentationController;
    //  设置Popover的代理, 取消modal的设备配置
    popover.delegate = self;
    //  设置来源视图/BarButtonItem 展示Popover之前, 必须设置参考点(参考的控件)
    popover.sourceView = sender;
    //  设置Popover的大小 preferredContentSize只能用于修改进行Popover展示的控制器的视图大小
    mapModeVc.preferredContentSize = CGSizeMake(300, 120);
    //  设置箭头方向
    popover.permittedArrowDirections = UIPopoverArrowDirectionDown;
    //  进行展示
    [self presentViewController:mapModeVc animated:YES completion:nil];
    
}

#pragma mark - UIAdaptivePresentationControllerDelegate
//  注意取消Modal自适应需要实现UIPopoverPresentationControllerDelegate的基协议中的方法
//  对Modal样式进行自适应时调用
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
    //  取消适配
    return UIModalPresentationNone;
}

#pragma mark - get & set
- (void)setTrack:(LKSportTrack *)track {
    
    _track = track;
    //  传递轨迹模型
    self.mapViewVC.track = track;
}

- (void)setTrackBtnCenter:(CGPoint)trackBtnCenter {
    
    _trackBtnCenter = trackBtnCenter;
    self.mapViewVC.trackBtnCenter = _trackBtnCenter;
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


@end
